package com.byb.server.service.impl;

import com.byb.server.service.ClaimsInstructionService;
import com.byb.server.domain.ClaimsInstruction;
import com.byb.server.repository.ClaimsInstructionRepository;
import com.byb.server.repository.search.ClaimsInstructionSearchRepository;
import com.byb.server.service.dto.ClaimsInstructionDTO;
import com.byb.server.service.mapper.ClaimsInstructionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing ClaimsInstruction.
 */
@Service
@Transactional
public class ClaimsInstructionServiceImpl implements ClaimsInstructionService{

    private final Logger log = LoggerFactory.getLogger(ClaimsInstructionServiceImpl.class);

    private final ClaimsInstructionRepository claimsInstructionRepository;

    private final ClaimsInstructionMapper claimsInstructionMapper;

    private final ClaimsInstructionSearchRepository claimsInstructionSearchRepository;
    public ClaimsInstructionServiceImpl(ClaimsInstructionRepository claimsInstructionRepository, ClaimsInstructionMapper claimsInstructionMapper, ClaimsInstructionSearchRepository claimsInstructionSearchRepository) {
        this.claimsInstructionRepository = claimsInstructionRepository;
        this.claimsInstructionMapper = claimsInstructionMapper;
        this.claimsInstructionSearchRepository = claimsInstructionSearchRepository;
    }

    /**
     * Save a claimsInstruction.
     *
     * @param claimsInstructionDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ClaimsInstructionDTO save(ClaimsInstructionDTO claimsInstructionDTO) {
        log.debug("Request to save ClaimsInstruction : {}", claimsInstructionDTO);
        ClaimsInstruction claimsInstruction = claimsInstructionMapper.toEntity(claimsInstructionDTO);
        claimsInstruction = claimsInstructionRepository.save(claimsInstruction);
        ClaimsInstructionDTO result = claimsInstructionMapper.toDto(claimsInstruction);
        //claimsInstructionSearchRepository.save(claimsInstruction);
        return result;
    }

    /**
     *  Get all the claimsInstructions.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ClaimsInstructionDTO> findAll(Pageable pageable) {
        log.debug("Request to get all ClaimsInstructions");
        return claimsInstructionRepository.findAll(pageable)
            .map(claimsInstructionMapper::toDto);
    }

    /**
     *  Get one claimsInstruction by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public ClaimsInstructionDTO findOne(Long id) {
        log.debug("Request to get ClaimsInstruction : {}", id);
        ClaimsInstruction claimsInstruction = claimsInstructionRepository.findOne(id);
        return claimsInstructionMapper.toDto(claimsInstruction);
    }

    /**
     *  Delete the  claimsInstruction by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete ClaimsInstruction : {}", id);
        claimsInstructionRepository.delete(id);
        //claimsInstructionSearchRepository.delete(id);
    }

    /**
     * Search for the claimsInstruction corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ClaimsInstructionDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of ClaimsInstructions for query {}", query);
        Page<ClaimsInstruction> result = claimsInstructionSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(claimsInstructionMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<ClaimsInstructionDTO> getAll(Long id, Pageable pageable) {
        log.debug("Request to  for a page of ClaimsInstructions for productId {}", id);
        Page<ClaimsInstruction> result = claimsInstructionRepository.findAllByProductId(id,pageable);
        return result.map(claimsInstructionMapper::toDto);
    }
}
